package com.imooc.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imooc.common.utils.common.io.IOUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.InputStreamReader;

/**
 * @author lihongyu
 * @CreateTime 2021-04-14 15:55
 * @Description 读取json文件工具类
 */
public class JsonFileUtils {

    /**
     * 读取JSON文件
     * @param path 文件路径
     * @return 文件内容
     */
    public static  <T>T readJsonFileForMap(String path, TypeReference<T> typeReference){
        try{
            ClassPathResource classPathResource = new ClassPathResource(path);
            String str = IOUtils.toString(new InputStreamReader(classPathResource.getInputStream(),"UTF-8"));
            return JSON.parseObject(str, typeReference);
        }catch (Exception e){
            throw new RuntimeException("读取JSON文件失败!");
        }
    }

}
